﻿#include "registerwin.h"
#include "ui_registerwin.h"
#include <QFaceObject.h>
#include <QFileDialog>
#include <QMessageBox>
#include <QSqlRecord>
#include <QSqlTableModel>
#include <QDebug>

RegisterWin::RegisterWin(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::RegisterWin)
{
    ui->setupUi(this);
}

RegisterWin::~RegisterWin()
{
    delete ui;
}

void RegisterWin::timerEvent(QTimerEvent *e)
{
    // if(cap.isOpened()){
    //     cap>>srcImage;
    // }
    if(cap.grab()){
        cap.read(srcImage);
    }
    if(srcImage.data==nullptr){
        return;
    }
    Mat rgbImage;//这里必须要用一个变量临时存储,不然存入的格式有问题
    //把opencv里面的mat的(BGR)转换成qt里面QImage(RGB)
    cvtColor(srcImage,rgbImage,COLOR_BGR2RGB);
    QImage image(rgbImage.data,rgbImage.cols,rgbImage.rows,rgbImage.step1(),QImage::Format_RGB888);

    QPixmap mmp=QPixmap::fromImage(image);
    mmp=mmp.scaled(ui->picLabel->size());
    ui->picLabel->setPixmap(mmp);
}



//清空输入框
void RegisterWin::on_resetPushButton_clicked()
{
    ui->nameLineEdit->clear();
    ui->birthdayTimeEdit->setDateTime(QDateTime::currentDateTime());
    ui->departmentLineEdit->clear();
    ui->phoneLineEdit->clear();
    ui->picpathLineEdit->clear();
}

//添加头像
void RegisterWin::on_addpicPushButton_clicked()
{
    QString picPath=QFileDialog::getOpenFileName();
    ui->picpathLineEdit->setText(picPath);
    //把头像显示
    QPixmap pix(picPath);
    pix=pix.scaled(QSize(ui->picLabel->size()));
    // pix = pix.scaledToWidth(ui->picLabel->width());
    ui->picLabel->setPixmap(pix);
}


void RegisterWin::on_registerPushButton_clicked()
{
    //把个人信息存储到数据库employee
    QSqlTableModel model;
    model.setTable("employee");//设置表名
    QSqlRecord record = model.record();
    //查询是否有重复的人名
    //给模型设置过滤器
    model.setFilter(QString("name='%1'").arg(ui->nameLineEdit->text()));
    //查询
    model.select();
    //判断是否查询到数据
    qDebug()<<"count";
    qDebug()<<model.rowCount();
    if(model.rowCount()>0){
        QMessageBox::warning(this,"警告","该员工已经注册");
        return;
    }

    //通过照片,结合faceObject模块得到faceID
    QFaceObject faceobject;
    cv::Mat image=cv::imread(ui->picpathLineEdit->text().toUtf8().toStdString());
    int faceId=faceobject.face_register(image);

    //把头像保存到一个固定路径下
    QString headPath=QString("./data/%1.jpg").arg(ui->nameLineEdit->text().toUtf8().toBase64().data());
    //创建data文件夹
    QDir dir("./");
    // 设置子目录名称为"data"
    QString dataDirPath = dir.filePath("data");

    // 检查data文件夹是否存在
    if (!dir.exists(dataDirPath)) {
        // 如果不存在，则创建data文件夹
        if (dir.mkdir(dataDirPath)) {
            qDebug() << "Data directory created successfully:" << dataDirPath;
        } else {
            qDebug() << "Failed to create data directory:" << dataDirPath;
        }
    } else {
        qDebug() << "Data directory already exists:" << dataDirPath;
    }
    //写图片到data文件夹
    if(image.empty()){
        return;
    }
    cv::imwrite(headPath.toUtf8().toStdString(),image);


    //设置数据
    record.setValue("name",ui->nameLineEdit->text());
    record.setValue("sex",ui->manRadioButton?"男":"女");
    record.setValue("birthday",ui->birthdayTimeEdit->text());
    record.setValue("department",ui->departmentLineEdit->text());
    record.setValue("phone",ui->phoneLineEdit->text());
    record.setValue("faceId",faceId);
    //头像路径
    record.setValue("headfile",headPath);
    //把记录插入数据库表格中
    bool ret=model.insertRecord(0,record);
    //提示注册成功
    if(ret){
        QMessageBox::information(this,"注册提示","注册成功");
        model.submit();
    }else{
        QMessageBox::information(this,"注册提示","注册失败");
    }
}


void RegisterWin::on_openCameraPushButton_clicked()
{
    if(ui->openCameraPushButton->text()=="打开摄像头"){

        //打开摄像头
        if(cap.open(0)){
            ui->openCameraPushButton->setText("关闭摄像头");
            //打开定时器
            timerid=startTimer(100);
        }

    }else{
        //关闭定时器
        killTimer(timerid);
        ui->openCameraPushButton->setText("打开摄像头");
        //关闭摄像头
        cap.release();
    }
}


void RegisterWin::on_catchpicPushButton_clicked()
{
    if(ui->nameLineEdit->text().isEmpty()){
        QMessageBox::warning(this,"警告","请先填写左边的信息");
        return;
    }
    //把头像保存到一个固定路径下
    QString headPath=QString("./data/%1.jpg").arg(ui->nameLineEdit->text().toUtf8().toBase64().data());
    ui->picpathLineEdit->setText(headPath);
    cv::imwrite(headPath.toUtf8().toStdString(),srcImage);
    //关闭定时器
    killTimer(timerid);
    ui->openCameraPushButton->setText("打开摄像头");
    //关闭摄像头
    cap.release();
}

